package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.base.BaseController;
import com.dzy.entity.Admin;
import com.dzy.entity.HouseBroker;
import com.dzy.service.AdminService;
import com.dzy.service.HouseBrokerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private AdminService adminService;
    @RequestMapping("/create")
    public String create(Long houseId,  Model model){
        List<Admin> adminList=this.adminService.findAll();
        model.addAttribute("adminList",adminList);
        model.addAttribute("houseId",houseId);
        return "houseBroker/create";
    }
    @RequestMapping("save")
    public String save(Long houseId,Long brokerId){
        //根据brokerId到acl_admin表中获取指定用户
        Admin admin = adminService.getById(brokerId);

        //创建HouseBroker对象
        HouseBroker houseBroker = new HouseBroker();
        houseBroker.setHouseId(houseId);
        houseBroker.setBrokerId(brokerId);
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());


        this.houseBrokerService.insert(houseBroker);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        //获取当前经纪人信息
        HouseBroker houseBroker = this.houseBrokerService.getById(id);
        model.addAttribute("houseBroker",houseBroker);
        //获取所有的用户列表
        List<Admin> adminList=this.adminService.findAll();
        model.addAttribute("adminList",adminList);
        return "houseBroker/edit";
    }

    @RequestMapping("update")
    public String update(HouseBroker houseBroker){
        //根据brokerId到acl_admin表中获取指定用户
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        this.houseBrokerService.update(houseBroker);
        return "common/successPage";
    }

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId,@PathVariable Long id){
        this.houseBrokerService.delete(id);
        return "redirect:/house/"+houseId;
    }
}
